using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using WPFServices.Commands;
using WPFServices.Models.Domain;
using WPFServices.Services.DomainService.WorkOrder;
using WPFServices.ViewModels;

namespace WPFServices.ViewModels.DomainViewModel.WorkOrder
{
    public class WorkOrderViewModel : BaseViewModel
    {
        private readonly WorkOrderService _workOrderService;
        private ObservableCollection<WorkOrderDto> _workOrders;
        private WorkOrderDto _selectedWorkOrder;
        private SearchWorkOrderDto _searchDto;
        private int _totalCount;
        private int _currentPage = 1;
        private int _pageSize = 10;
        private bool _isLoading;

        public WorkOrderViewModel()
        {
            _workOrderService = new WorkOrderService();
            _workOrders = new ObservableCollection<WorkOrderDto>();
            _searchDto = new SearchWorkOrderDto
            {
                PageIndex = 1,
                PageSize = 10
            };

            LoadDataCommand = new RelayCommand(async _ => await LoadDataAsync());
            SearchCommand = new RelayCommand(async _ => await SearchAsync());
            ResetCommand = new RelayCommand(_ => Reset());
            PreviousPageCommand = new RelayCommand(async _ => await PreviousPageAsync(), _ => CanPreviousPage());
            NextPageCommand = new RelayCommand(async _ => await NextPageAsync(), _ => CanNextPage());

            // 初始加载数据
            _ = LoadDataAsync();
        }

        #region 属性
        public ObservableCollection<WorkOrderDto> WorkOrders
        {
            get => _workOrders;
            set => SetProperty(ref _workOrders, value);
        }

        public WorkOrderDto SelectedWorkOrder
        {
            get => _selectedWorkOrder;
            set => SetProperty(ref _selectedWorkOrder, value);
        }

        public SearchWorkOrderDto SearchDto
        {
            get => _searchDto;
            set => SetProperty(ref _searchDto, value);
        }

        public int TotalCount
        {
            get => _totalCount;
            set => SetProperty(ref _totalCount, value);
        }

        public int CurrentPage
        {
            get => _currentPage;
            set => SetProperty(ref _currentPage, value);
        }

        public int PageSize
        {
            get => _pageSize;
            set => SetProperty(ref _pageSize, value);
        }

        public bool IsLoading
        {
            get => _isLoading;
            set => SetProperty(ref _isLoading, value);
        }

        public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);
        #endregion

        #region 命令
        public RelayCommand LoadDataCommand { get; }
        public RelayCommand SearchCommand { get; }
        public RelayCommand ResetCommand { get; }
        public RelayCommand PreviousPageCommand { get; }
        public RelayCommand NextPageCommand { get; }
        #endregion

        #region 方法
        private async Task LoadDataAsync()
        {
            try
            {
                IsLoading = true;
                SearchDto.PageIndex = CurrentPage;
                SearchDto.PageSize = PageSize;

                var result = await _workOrderService.GetWorkOrderListAsync(SearchDto);
                
                WorkOrders.Clear();
                foreach (var workOrder in result.Data)
                {
                    WorkOrders.Add(workOrder);
                }
                TotalCount = result.TotalCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载工单数据失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                IsLoading = false;
            }
        }

        private async Task SearchAsync()
        {
            CurrentPage = 1;
            await LoadDataAsync();
        }

        private void Reset()
        {
            SearchDto = new SearchWorkOrderDto
            {
                PageIndex = 1,
                PageSize = PageSize
            };
            CurrentPage = 1;
            _ = LoadDataAsync();
        }

        private async Task PreviousPageAsync()
        {
            if (CurrentPage > 1)
            {
                CurrentPage--;
                await LoadDataAsync();
            }
        }

        private async Task NextPageAsync()
        {
            if (CurrentPage < TotalPages)
            {
                CurrentPage++;
                await LoadDataAsync();
            }
        }

        private bool CanPreviousPage()
        {
            return CurrentPage > 1;
        }

        private bool CanNextPage()
        {
            return CurrentPage < TotalPages;
        }
        #endregion
    }
} 